package com.yangzheandroid.rxmvp.presenter.login;

import com.yangzheandroid.retrofitutils.utils.CountNumTimer;
import com.yangzheandroid.rxmvp.base.RxPresenter;
import com.yangzheandroid.rxmvp.beans.UserBean;
import com.yangzheandroid.rxmvp.http.DefaultSubscriber;
import com.yangzheandroid.rxmvp.http.HttpClients;
import com.yangzheandroid.rxmvp.utils.ToastUtils;
import com.yangzheandroid.rxmvp.view.activity.login.RegisterContract;

import javax.inject.Inject;

import rx.Subscription;

/**
 * Author：Jalen on 2016/7/17 13:01
 * Contact: studylifetime@sina.com
 */
public class RegisterPresenter extends RxPresenter<RegisterContract.View> implements RegisterContract.Presenter {
    private static final String TAG = RegisterPresenter.class.getSimpleName();
    RegisterContract.View mView;
    private int Count = 60;
    @Inject
    public RegisterPresenter() {

    }

    @Override
    public void register() {

        Subscription subscribe = HttpClients.subscribe(HttpClients.apiStore().register(mView.getUserName(), "123456", mView.getSmsCode()), new DefaultSubscriber<UserBean>() {
            @Override
            public void onNext(UserBean userBean) {
                ToastUtils.showToast("注册成功  " + userBean.getRetCode());
            }
        });
     addSubscribe(subscribe);

    }

    @Override
    public void getSmsCode() {
        new CountNumTimer(60000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                Count--;
                mView.changeSmsText(Count + "后重新发送");
                mView.setClickable(false);
            }

            @Override
            public void onFinish() {
                mView.changeSmsText("重新获取验证码");
                mView.setClickable(true);
                Count = 60;
            }
        }.start();

//        mRegisterResponstory.getSmsCode(mView.getSmsCode())
//                .compose(RxSchedulersHelper.<Integer>io_main())
//                .doOnSubscribe(new Action0() {
//                    @Override
//                    public void call() {
//                        mView.showLoading();
//                    }
//                }).subscribe(new Subscriber<Integer>() {
//            @Override
//            public void onCompleted() {
//                mView.hideLoading();
//            }
//
//            @Override
//            public void onError(Throwable throwable) {
//                ToastUtils.showToast("注册失败");
//                mView.hideLoading();
//            }
//
//            @Override
//            public void onNext(Integer integer) {
//                mView.hideLoading();
//            }
//        });

    }


}
